package com.example.user.client;

import com.example.user.dto.ProductDTO;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @title: ProductClientFallbackFactory
 * @description: fallback factory用于检查回退原因
 * @author: brucewong
 * @version: 1.0
 * @date：2019/10/12 21:48
 */
@Slf4j
@Component
public class ProductClientFallbackFactory  implements FallbackFactory<ProductClient> {
    @Override
    public ProductClient create(Throwable throwable) {
        return new ProductClient() {
            @Override
            public ProductDTO findProduct(String id) {
                log.info("ProductClient.findProduct(id) fallback, reason was: {}", throwable.getMessage() );
                ProductDTO productDTO = new ProductDTO();
                productDTO.setId("-1");
                productDTO.setName("默认");
                return productDTO;
            }

            @Override
            public ProductDTO findProduct(String id, String name) {
                log.info("ProductClient.findProduct(id, name) fallback, reason was: {}", throwable.getMessage() );
                ProductDTO productDTO = new ProductDTO();
                productDTO.setId("-1");
                productDTO.setName("默认");
                return productDTO;
            }

            @Override
            public ProductDTO add(ProductDTO productDTO) {
                log.info("ProductClient.add() fallback, reason was: {}", throwable.getMessage() );
                productDTO = new ProductDTO();
                productDTO.setId("-1");
                productDTO.setName("默认");
                return productDTO;
            }
        };
    }
}